﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecommerce.Model;

namespace Ecommerce.Admin.Controles
{
    public partial class P_InfoObservacao : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["IdPed"] != null)
                {
                    int IdPed = Convert.ToInt32(Request.QueryString["IdPed"]);
                    hddNumPedido.Value = IdPed.ToString();
                    CarregarLog(IdPed);
                }
            }
        }

        private void CarregarLog(int IdPed)
        {
            var log = PedidoModel.Instance.ListarLogPedido(IdPed);
            grvLog.DataSource = log;
            grvLog.DataBind();
        }
        
        protected void grvLog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var log = (tb_lrp_log_rastreamento_pedido)e.Row.DataItem;
                e.Row.Cells[0].Text = log.lrp_dat_criacao.ToString();
                e.Row.Cells[1].Text = log.lrp_des_log_rastreio_pedido;
                
                LinkButton lkbEditar = (LinkButton)e.Row.FindControl("lkbExcluir");
                lkbEditar.Text = "Excluir";
            }
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            var log = new tb_lrp_log_rastreamento_pedido();
            log.lrp_dat_criacao = DateTime.Now;
            log.lrp_des_log_rastreio_pedido = txtNota.Text;
            log.ped_idt_pedido = Convert.ToInt32(hddNumPedido.Value);
            PedidoModel.Instance.InserirLogPedido(log);

            this.CarregarLog(log.ped_idt_pedido);

            txtNota.Text = string.Empty;
        }
    }
}